package com.dd.machine.base.utils;

import android.os.Environment;

import java.io.File;

public class SDCardUtils {

    public static String getSDPath() {
        String result = "";
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();
        } else {
            sdDir = Environment.getDataDirectory();
        }

        result = sdDir.toString();

        return result;
    }


    public static String getImageIconPath(String fileName) {

        File file = new File(getSDPath() + "/.bornwriter/");
        DeleteFile(file);

        return getSDPath() + "/.bornwriter/"+fileName+".png";//以name存在目录中
    }

    /**
     * 递归删除文件和文件夹
     *
     * @param file 要删除的根目录
     */
    private static void DeleteFile(File file) {
        if (file.exists() == false) {
            file.mkdir();
            return;
        } else {
            if (file.isFile()) {
                file.delete();
                return;
            }
            if (file.isDirectory()) {
                File[] childFile = file.listFiles();
                if (childFile == null || childFile.length == 0) {
                    file.delete();
                    return;
                }
                for (File f : childFile) {
                    DeleteFile(f);
                }
            }
        }
    }

}
